package cn.tedu.blog.server.controller.system;




import cn.tedu.blog.server.pojo.dto.MenuAddNewDTO;
import cn.tedu.blog.server.pojo.vo.MenuListVO;
import cn.tedu.blog.server.pojo.vo.MenusTreeVO;
import cn.tedu.blog.server.service.IMenuService;
import cn.tedu.blog.server.utils.JsonResult;
import cn.tedu.blog.server.utils.JsonResults;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 雷茂林
 * @description:
 * @date
 */

/**
 * @author blue
 * @description: 后台系统菜单管理控制器
 * @date
 */
@Api(tags = "3. 菜单管理")
@Slf4j
@RestController
@RequestMapping("/system/menu")
public class MenuController {


    @Autowired
    private IMenuService menuService;


    @ApiOperation("创建菜单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/createMenu")
    @PreAuthorize("hasAuthority('/system/menu/createMenu')")
    public JsonResult createMenu(@RequestBody MenuAddNewDTO menuAddNewDTO){
        log.debug("接收到客户端提交的创建菜单请求，参数：{}",menuAddNewDTO);
        menuService.createMenu(menuAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("删除菜单")
    @ApiOperationSupport(order = 200)
    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('/system/menu/deleteById')")
    public JsonResult deleteById(@RequestBody MenuAddNewDTO menuAddNewDTO){
        log.debug("接收到客户端提交的删除菜单请求，参数：{}",menuAddNewDTO.getId());
        menuService.deleteById(menuAddNewDTO.getId());
        return JsonResult.ok();
    }



    @ApiOperation("批量删除菜单")
    @ApiOperationSupport(order = 201)
    @PostMapping("/deleteByIds")
    @PreAuthorize("hasAuthority('/system/menu/deleteByIds')")
    public JsonResult deleteByIds(@RequestBody List<Long> ids){
        log.debug("接收到客户端提交的【批量删除菜单】的请求，参数：{}",ids);
        menuService.deleteByIds(ids);
        return JsonResult.ok();
    }



    @ApiOperation("修改菜单")
    @ApiOperationSupport(order = 300)
    @PostMapping("/updateById")
    @PreAuthorize("hasAuthority('/system/menu/updateById')")
    public JsonResult updateById(@RequestBody MenuAddNewDTO menuAddNewDTO){
        log.debug("接收到客户端提交的修改菜单请求，参数：{}",menuAddNewDTO);
        menuService.updateById(menuAddNewDTO);
        return JsonResult.ok();
    }


    @ApiOperation("启用/禁用权限")
    @ApiOperationSupport(order = 300)
    @PostMapping("/switchPermission")
    @PreAuthorize("hasAuthority('/system/menu/switchPermission')")
    public JsonResult switchPermission(@RequestBody MenuAddNewDTO menuAddNewDTO){
        log.debug("接收到客户端提交的【启用/禁用权限】请求，参数：{}",menuAddNewDTO);
        menuService.switchPermission(menuAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("查询菜单")
    @ApiOperationSupport(order = 400)
    @PostMapping("/listMenusByCondition")
    public List<MenuListVO> listMenusByCondition(@RequestBody MenuListVO menuListVO){
        log.debug("接收到客户端提交的条件分页查询菜单请求，参数：{}",menuListVO);
        return menuService.listByKeyword(menuListVO);
    }



    @ApiOperation("查询更换父级权限后是否会超越三级菜单定义")
    @ApiOperationSupport(order = 401)
    @GetMapping("/isViolationMenuTree")
    @PreAuthorize("hasAuthority('/system/menu/isViolationMenuTree')")
    public JsonResult isViolationMenuTree(Long menuId ,Long parentId){
        log.debug("接收到客户端提交的【查询id为【"+menuId+"】的权限更换父级权限后是否会超越三级菜单定义】请求，此次请求更换父级id为：{}",parentId);
        menuService.isViolationMenuTree(menuId,parentId);
        return JsonResult.ok();
    }



    @PostMapping("/getCountAll")
    public int getCountAll(@RequestBody MenuListVO userListVO){
        log.debug("【countAll】接收到客户端提交的请求，参数：{}",userListVO);
        return menuService.getCountAll(userListVO);
    }



    @GetMapping("/getBackFillById")
    public MenuListVO getBackFillById(Long id){
        log.debug("接收到客户端提交的请求，参数：{}",id);
        return menuService.getBackFillById(id);
    }



    @GetMapping("/getTreeVOAll")
    @ApiOperation("查询所有三级分类树")
    public JsonResults<MenusTreeVO<MenuListVO>> getTreeVO() {
        MenusTreeVO<MenuListVO> treeVO = menuService.categoryTree();
//        System.out.println("我是三级菜单："+treeVO);
        return JsonResults.ok(treeVO);
    }


    @ApiOperation("判断当前id是否含有子级权限")
    @ApiOperationSupport(order = 501)
    @GetMapping("/isSonMenuById")
    public JsonResult isSonMenuById(Long id){
        log.debug("【判断当前 id={} 是否含有子级权限】",id);
        menuService.isSonMenuById(id);
        return JsonResult.ok();
    }
}